﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework
Imports Runa.UI.Animation
Imports Runa.Graphics.Effects
Imports Runa.Core.Services
Imports Runa.Foundation.Diagnostics

Namespace Global.Runa.UI.Effects

    Public Class WpfEffectBase
        Inherits Runa.Graphics.Effects.EffectBase
        Implements IAnimatable
#If Not WINDOWS_PHONE Then
        Implements ICloneable
#End If

        Public Event AnimationBegin(sender As Object, e As System.EventArgs(Of Animation.AnimationBase)) Implements Animation.IAnimatable.AnimationBegin
        Public Event AnimationEnd(sender As Object, e As System.EventArgs(Of Animation.AnimationBase)) Implements Animation.IAnimatable.AnimationEnd

        Protected _Progress As Single

        Protected ReadOnly Property GameParameterService() As IGameParameterService
            Get
                Return Me.Game.Services.GetService(GetType(IGameParameterService))
            End Get
        End Property

        Private _Animations As List(Of AnimationBase) = New List(Of AnimationBase)

        Public Sub New(ByVal g As Game)
            MyBase.New(g, GetType(WpfEffectBase).Assembly)
        End Sub

        Public Sub New(ByVal g As Game, ByVal e As Effect)
            MyBase.New(g)
            Me.Effect = e
        End Sub

        Public Overrides Sub UpdataParameters(ByVal Gametime As GameTime)
            For i As Integer = 0 To Me.Animations.Count - 1
                Try
                    Me.Animations(i).Updata(Gametime)
                Catch ex As Exception
                    Continue For
                End Try
            Next
        End Sub

        Public Overrides Property Effect() As Microsoft.Xna.Framework.Graphics.Effect
            Get
                Return MyBase.Effect
            End Get
            Set(value As Microsoft.Xna.Framework.Graphics.Effect)
                If Me.GameParameterService.IsHiDefSupported Then
                    MyBase.Effect = value
                Else
                    Tracer.Default.TraceMessage("Warning: Under Reach Profile, some effect may not run properly.")
                End If
                Me.Opacity = 1.0!
            End Set
        End Property

        Public Property Animations As List(Of AnimationBase) Implements IAnimatable.Animations
            Get
                Return Me._Animations
            End Get
            Set(ByVal value As List(Of AnimationBase))
                Me._Animations = value
            End Set
        End Property

        Public Overridable Property Progress As Single
            Get
                Return Me._Progress
            End Get
            Set(ByVal value As Single)
                If (((Me._Progress <> value) AndAlso (value >= 0.0!)) AndAlso (value <= 1.0!)) Then
                    Me._Progress = value
                    MyBase.Effect.Parameters("Progress").SetValue(value)
                End If
            End Set
        End Property

        Public Sub OnAnimationBegin(sender As Object, e As System.EventArgs(Of Animation.AnimationBase)) Implements Animation.IAnimatable.OnAnimationBegin
            RaiseEvent AnimationBegin(sender, e)
        End Sub

        Public Sub OnAnimationEnd(sender As Object, e As System.EventArgs(Of Animation.AnimationBase)) Implements Animation.IAnimatable.OnAnimationEnd
            RaiseEvent AnimationEnd(sender, e)
        End Sub

#If Not WINDOWS_PHONE Then
        Public Function Clone() As Object Implements System.ICloneable.Clone
            Return New WpfEffectBase(Me.Game, Me.Effect.Clone())
        End Function
#End If

    End Class

End Namespace